package com.javaweb.oa02.web.action.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class loginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        //先要做强制类型转换
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        /**
         * 什么情况下不能拦截？（目前写的路径是：/* 表示所有的请求均拦截。）
         *      用户访问 index.jsp的时候不能拦截
         *      用户已经登录了，这个需要放行，不能拦截。
         *      用户要去登录，这个也不能拦截。
         *      WelcomeServlet也不能拦截。
         */
        String servletPath=request.getServletPath();
        HttpSession session=request.getSession(false);
//        if("/index.jsp".equals(servletPath) || "/welcome".equals(servletPath) ||
//                "/user/login".equals(servletPath) || "/user/exit".equals(servletPath)
//                || (session != null && session.getAttribute("user") != null)){
               // 继续往下走
        if("/index.jsp".equals(servletPath) || "/welcome".equals(servletPath) ||
                "/user/login".equals(servletPath) || "/user/exit".equals(servletPath)
                || (session != null && session.getAttribute("user1") != null)){
                 //放行不拦截
                filterChain.doFilter(request, response);
        }else{
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }

    }

    @Override
    public void destroy() {

    }
}
